﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using PPAP.BO;

namespace PPAP.Web.Master
{
    public partial class PPAPNewsMore : PageBase
    {
        PPAPNewsBO mbo = new PPAPNewsBO();
        LogBO lbo = new LogBO();
        UsersBO ubo = new UsersBO();
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                //if (!ubo.IsAdmin(CurrentUser.Id))
                //{
                //    td1.Visible = false;
                //    td2.Visible = false;
                //    GridView1.Columns[3].Visible = false;
                //    GridView1.Columns[7].Visible = false;
                //    GridView1.Columns[8].Visible = false;
                //}
                LoadData();

                PPAP.Model.PPAPNews p = mbo.GetModel(1);
                if (p != null)
                {
                    lblWelcome.Text = p.CONTENT;
                }
            }
            if (Request.QueryString["deid"] != null)
            {
                dele(Request.QueryString["deid"].ToString());
            }
        }

        private void dele(string id)
        {
            int num = mbo.Delete(int.Parse(id));
            lbo.WriteLog(CurrentUser.Id, "Delete MailTemplate");
            if (num > 0)
                AlertMessage("Deleted successful");
            LoadData();
        }

        private void LoadData()
        {
            DataSet ds = mbo.GetList(" id<>1");
            //if (!ubo.IsAdmin(CurrentUser.Id))
            //{
                if (CurrentUser.UnitId == "0" || CurrentUser.UnitId == "99") //供应商
                {
                    ds = mbo.GetList("  id<>1  and( type=2 or type=3) ");  //供应商只能看到For BDNT& supplier
                }
                else
                {
                    ds = mbo.GetList("  id<>1  and( type=2 or type=1)  ");  //其他人可以看到全部
                }
            //}
            AspNetPager2.RecordCount = ds.Tables[0].Rows.Count;
            DataTable dt = GetPageTable(ds.Tables[0], AspNetPager2.PageSize, AspNetPager2.CurrentPageIndex);
            GridView1.DataSource = GetPageTable(ds.Tables[0], AspNetPager2.PageSize, AspNetPager2.CurrentPageIndex);
            GridView1.DataBind();

            if (dt.Rows.Count > 0)
            {
                tr1.Visible = true;
                Label1.Text = string.Format("<a href='#' onclick='javascript:view({0});' title='Click me'   style=' font-size:10pt; font-family:Arial;margin-top:10px;' >{1}</a>", dt.Rows[0]["id"].ToString(), DateTime.Parse(dt.Rows[0]["CREATETIME"].ToString()).ToString("yyyy-MM-dd") + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + dt.Rows[0]["TITLE"].ToString());
            }
            else
                tr1.Visible = false;

            if (dt.Rows.Count > 1)
            {
                tr2.Visible = true;
                Label2.Text = string.Format("<a href='#' onclick='javascript:view({0});' title='Click me'   style=' font-size:10pt; font-family:Arial;margin-top:10px;' >{1}</a>", dt.Rows[1]["id"].ToString(), DateTime.Parse(dt.Rows[1]["CREATETIME"].ToString()).ToString("yyyy-MM-dd") + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + dt.Rows[1]["TITLE"].ToString());
            }
            else
                tr2.Visible = false;

            if (dt.Rows.Count > 2)
            {
                tr3.Visible = true;
                Label3.Text = string.Format("<a href='#' onclick='javascript:view({0});' title='Click me'    style=' font-size:10pt; font-family:Arial; margin-top:10px;'>{1}</a>", dt.Rows[2]["id"].ToString(), DateTime.Parse(dt.Rows[2]["CREATETIME"].ToString()).ToString("yyyy-MM-dd") + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + dt.Rows[2]["TITLE"].ToString());
            }
            else
                tr3.Visible = false;

            if (dt.Rows.Count > 3)
            {
                tr4.Visible = true;
                Label4.Text = string.Format("<a href='#' onclick='javascript:view({0});' title='Click me'   style=' font-size:10pt; font-family:Arial;margin-top:10px;' >{1}</a>", dt.Rows[3]["id"].ToString(), DateTime.Parse(dt.Rows[3]["CREATETIME"].ToString()).ToString("yyyy-MM-dd") + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + dt.Rows[3]["TITLE"].ToString());
            }
            else
                tr4.Visible = false;

            if (dt.Rows.Count > 4)
            {
                tr5.Visible = true;
                Label5.Text = string.Format("<a href='#' onclick='javascript:view({0});' title='Click me'   style=' font-size:10pt; font-family:Arial;margin-top:10px;' >{1}</a>", dt.Rows[4]["id"].ToString(), DateTime.Parse(dt.Rows[4]["CREATETIME"].ToString()).ToString("yyyy-MM-dd") + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + dt.Rows[4]["TITLE"].ToString());
            }
            else
                tr5.Visible = false;

            if (dt.Rows.Count > 5)
            {
                tr6.Visible = true;
                Label6.Text = string.Format("<a href='#' onclick='javascript:view({0});' title='Click me'   style=' font-size:10pt; font-family:Arial;margin-top:10px;' >{1}</a>", dt.Rows[5]["id"].ToString(), DateTime.Parse(dt.Rows[5]["CREATETIME"].ToString()).ToString("yyyy-MM-dd") + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + dt.Rows[5]["TITLE"].ToString());
            }
            else
                tr6.Visible = false;

            if (dt.Rows.Count > 6)
            {
                tr7.Visible = true;
                Label7.Text = string.Format("<a href='#' onclick='javascript:view({0});' title='Click me'   style=' font-size:10pt; font-family:Arial;margin-top:10px;' >{1}</a>", dt.Rows[6]["id"].ToString(), DateTime.Parse(dt.Rows[6]["CREATETIME"].ToString()).ToString("yyyy-MM-dd") + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + dt.Rows[6]["TITLE"].ToString());
            }
            else
                tr7.Visible = false;

            if (dt.Rows.Count > 7)
            {
                tr8.Visible = true;
                Label8.Text = string.Format("<a href='#' onclick='javascript:view({0});' title='Click me'   style=' font-size:10pt; font-family:Arial;margin-top:10px;' >{1}</a>", dt.Rows[7]["id"].ToString(), DateTime.Parse(dt.Rows[7]["CREATETIME"].ToString()).ToString("yyyy-MM-dd") + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + dt.Rows[7]["TITLE"].ToString());
            }
            else
                tr8.Visible = false;

            if (dt.Rows.Count > 8)
            {
                tr9.Visible = true;
                Label9.Text = string.Format("<a href='#' onclick='javascript:view({0});' title='Click me'   style=' font-size:10pt; font-family:Arial;margin-top:10px;' >{1}</a>", dt.Rows[8]["id"].ToString(), DateTime.Parse(dt.Rows[8]["CREATETIME"].ToString()).ToString("yyyy-MM-dd") + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + dt.Rows[8]["TITLE"].ToString());
            }
            else
                tr9.Visible = false;

            if (dt.Rows.Count > 9)
            {
                tr10.Visible = true;
                Label10.Text = string.Format("<a href='#' onclick='javascript:view({0});' title='Click me'   style=' font-size:10pt; font-family:Arial;margin-top:10px;' >{1}</a>", dt.Rows[9]["id"].ToString(), DateTime.Parse(dt.Rows[9]["CREATETIME"].ToString()).ToString("yyyy-MM-dd") + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + dt.Rows[9]["TITLE"].ToString());
            }
            else
                tr10.Visible = false;
        }

        protected void AspNetPager2_PageChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        //private void LoadDep(string id)
        //{
        //    Model.Department d = bo.GetModel(int.Parse(id));
        //    txtDepartment.Text = d.DEPARTMENT;
        //    txtDescription.Text = d.DESCRIPTION;
        //    Button1.Text = "Save";
        //}

        //protected void Button1_Click(object sender, EventArgs e)
        //{
        //    Model.Department d = new Model.Department();
        //    int num = 0;
        //    if (txtDescription.Text.Trim() == "" || txtDepartment.Text.Trim() == "")
        //    {
        //        AlertMessage("The text frame not is empty");
        //        return;
        //    }
        //    d.DESCRIPTION = txtDescription.Text;
        //    d.DEPARTMENT = txtDepartment.Text;
        //    d.CREATETIME = DateTime.Now;
        //    d.CREATEDBY = CurrentUser.Name;
        //    if (Button1.Text == "Add")
        //    {
        //        num = bo.Add(d);
        //        lbo.WriteLog(CurrentUser.Name, "Add Department:" + d.DEPARTMENT);
        //    }
        //    else
        //    {
        //        d.ID = int.Parse(Request.QueryString["DID"].ToString());
        //        num = bo.Update(d);
        //        lbo.WriteLog(CurrentUser.Name, "Edit Department:" + d.DEPARTMENT);
        //    }
        //    if (num > 0)
        //    {
        //        txtDepartment.Text = "";
        //        txtDescription.Text = "";
        //        Button1.Text = "Add";
        //        //AlertMessage("Save success");
        //    }
        //    LoadData();
        //}

    }
}